package icegps.com.pingdj.activity;


import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;

import icegps.com.pingdj.R;
import icegps.com.pingdj.adapter.MapInfoAdapter;
import icegps.com.pingdj.bean.DeviceStatus;
import icegps.com.pingdj.bean.KeyValue;
import icegps.com.pingdj.bean.LocationStatus;
import icegps.com.pingdj.data.ble.WriteHelper;
import icegps.com.pingdj.map.listener.OnSaveBitmapListener;
import icegps.com.pingdj.map.utils.DispersionToColor;
import icegps.com.pingdj.map.utils.LatAndLot2Coord;
import icegps.com.pingdj.utils.StringUtils;
import icegps.com.pingdj.utils.UnitUtils;

import static icegps.com.pingdj.constant.ActionKey.BPOS;
import static icegps.com.pingdj.constant.ActionKey.LAT;
import static icegps.com.pingdj.constant.ActionKey.LNG;
import static icegps.com.pingdj.constant.ActionKey.SAVE_TOKEN;
import static icegps.com.pingdj.constant.ActionKey.TITLE;
import static icegps.com.pingdj.constant.ActionKey.LOAD_TOKEN;

public class WorkMapActivity extends BaseMapActivity implements View.OnClickListener {
    private ArrayList<KeyValue> menuA;
    private ArrayList<KeyValue> menuB;
    private MapInfoAdapter mapInfoAdapter1;
    private MapInfoAdapter mapInfoAdapter2;
    private LatAndLot2Coord latAndLot2Coord;
    private int insMode = -1;
    private double lat;
    private double lng;
    private double distance;
    private double speed;

    private long loadToken;

    @Override
    protected int setLayout() {
        return R.layout.activity_map;
    }

    @Override
    protected String setTitle() {
        return getIntent().getStringExtra(TITLE);
    }

    @Override
    protected void init() {
        super.init();
        latAndLot2Coord = new LatAndLot2Coord();
        menuA = new ArrayList<>();
        menuB = new ArrayList<>();
        tvMainAntenna.setVisibility(View.VISIBLE);
        tvDeputyAntenna.setVisibility(View.VISIBLE);
        ivMainAction.setVisibility(View.VISIBLE);
        ivDeputyAction.setVisibility(View.VISIBLE);
        initRecycleview();
        setListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WriteHelper.write(WriteHelper.GRADER + "0");
    }

    @Override
    protected long getSaveToken() {
        return getIntent().getLongExtra(SAVE_TOKEN, System.currentTimeMillis());
    }

    @Override
    protected long getLoadToken() {
        loadToken = getIntent().getLongExtra(LOAD_TOKEN, System.currentTimeMillis());
        return loadToken;
    }


    private void initRecycleview() {
        initLeftMenu();
        recyclerView1.setLayoutManager(new LinearLayoutManager(this));
        mapInfoAdapter1 = new MapInfoAdapter(R.layout.item_key_value, menuA);
        recyclerView1.setAdapter(mapInfoAdapter1);
        recyclerView2.setLayoutManager(new LinearLayoutManager(this));
        mapInfoAdapter2 = new MapInfoAdapter(R.layout.item_key_value, menuB);
        recyclerView2.setAdapter(mapInfoAdapter2);
    }

    private void initLeftMenu() {
        menuA.add(new KeyValue(TRAGET_HEIGHT, initValue, heightUnit));
        menuA.add(new KeyValue(C_H, initValue, heightUnit));
        menuA.add(new KeyValue(DIFFERENCE, initValue, heightUnit));
        menuA.add(new KeyValue(DISTANCE, initValue, lengthUnit));
        menuA.add(new KeyValue(SPEED, initValue, speedUnit));
        lat = getIntent().getDoubleExtra(LAT, 0);
        lng = getIntent().getDoubleExtra(LNG, 0);
    }


    private void setListener() {
        tvEnlarge.setOnClickListener(this);
        tvNarrow.setOnClickListener(this);
        ivLeftOrRight.setOnClickListener(this);
        mapView.setOnMapMultipleListener(onMapMultipleListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapManager.loadTile();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapManager.saveData(null, false);

    }

    @Override
    protected boolean pauseIsSaveBitmap() {
        return true;
    }

    @Override
    protected boolean backIsSaveBitmap() {
        return true;
    }

    @Override
    public void onAnalysisHexLocationData(LocationStatus locationStatus) {
        super.onAnalysisHexLocationData(locationStatus);
        locationStatus.setColor(DispersionToColor.getcolor(locationStatus.getDifference()));
        mapManager.refreshData(latAndLot2Coord.getTargetPoint(locationStatus, loadToken));
        if (lat == 0) lat = locationStatus.getLatitude();
        if (lng == 0) lng = locationStatus.getLongitude();
        distance = getDistance(lat, lng, locationStatus.getLatitude(), locationStatus.getLongitude());
        speed = locationStatus.getSpeed();
    }


    @Override
    public void onAnalysisAscDeviceStatus(DeviceStatus deviceStatus) {
        super.onAnalysisAscDeviceStatus(deviceStatus);
        setIvSrc(ivDeputyAction, deviceStatus.getControlTwo());
        setIvSrc(ivMainAction, deviceStatus.getControlOne());
        if (insMode != deviceStatus.getInsMode()) {
            insMode = deviceStatus.getInsMode();
            setMenuBElement(insMode);
        }
        for (KeyValue keyValue : menuA) {
            String key = keyValue.getKey();
            if (key.equals(TRAGET_HEIGHT)) {
                String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), deviceStatus.getTragetHeight1());
                keyValue.setValue(conversion);
            }
            if (key.equals(C_H)) {
                //现高=标高+高差
                double ch = deviceStatus.getTragetHeight1() + deviceStatus.getCurrentTimedifference1();
                String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), ch);
                keyValue.setValue(conversion);
            }
            if (key.equals(DIFFERENCE)) {
                String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), deviceStatus.getCurrentTimedifference1());
                keyValue.setValue(conversion);
            }
            if (key.equals(DISTANCE)) {
                String conversion = UnitUtils.conversion(UnitUtils.LENGTH, deviceStatus.getLength(), distance);
                keyValue.setValue(conversion);
            }
            if (key.equals(SPEED)) {
                String conversion = UnitUtils.conversion(UnitUtils.SPEED, deviceStatus.getSpeed(), speed);
                keyValue.setValue(conversion);
            }
        }
        for (KeyValue keyValue : menuB) {
            String key = keyValue.getKey();
            if (key.equals(TRAGET_HEIGHT)) {
                String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), deviceStatus.getTragetHeight2());
                keyValue.setValue(conversion);
            }
            if (key.equals(C_H)) {
                //现高=标高+高差
                double ch = deviceStatus.getTragetHeight2() + deviceStatus.getCurrentTimedifference2();
                String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), ch);
                keyValue.setValue(conversion);
            }
            if (key.equals(DIFFERENCE)) {
                String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), deviceStatus.getCurrentTimedifference2());
                keyValue.setValue(conversion);
            }

            if (key.equals(BALANCE)) {
                keyValue.setValue(StringUtils.setAccuracy(deviceStatus.getTragetHeight2(), 3));
            }
            if (key.equals(CURRENT)) {
                double current = deviceStatus.getTragetHeight2() + deviceStatus.getCurrentTimedifference2();
                keyValue.setValue(StringUtils.setAccuracy(current, 3));
            }
            if (key.equals(SLOPE)) {
                keyValue.setValue(StringUtils.setAccuracy(deviceStatus.getCurrentTimedifference2(), 3));
            }
        }
        mapInfoAdapter1.notifyDataSetChanged();
        mapInfoAdapter2.notifyDataSetChanged();
    }

    private void setMenuBElement(int insMode) {
        menuB.clear();
        if (insMode == 0) {
            tvDeputyAntenna.setVisibility(View.GONE);
            ivDeputyAction.setVisibility(View.GONE);
        }
        if (insMode == 1) {
            menuB.add(new KeyValue(TRAGET_HEIGHT, initValue, heightUnit));
            menuB.add(new KeyValue(C_H, initValue, heightUnit));
            menuB.add(new KeyValue(DIFFERENCE, initValue, heightUnit));
        }
        if (insMode == 2) {
            menuB.add(new KeyValue(BALANCE, initValue, "°"));
            menuB.add(new KeyValue(CURRENT, initValue, "°"));
            menuB.add(new KeyValue(SLOPE, initValue, "°"));
        }
    }

    public void setIvSrc(ImageView iv, int action) {
        switch (action) {
            case 0:
            case 1:
                iv.setImageResource(R.drawable.svg_no_action);
                break;
            case 2:
                iv.setImageResource(R.drawable.svg_down);
                break;
            case 3:
                iv.setImageResource(R.drawable.svg_up);
                break;
        }
    }

    //单位:米
    private double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2.0 *
                Math.asin(
                        Math.sqrt(
                                Math.pow(
                                        Math.sin(a / 2.0), 2.0) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2.0), 2.0)));
        s *= EARTH_RADIUS;
        return s;
    }

    private float EARTH_RADIUS = 6378137.0f;

    private double rad(double d) {
        return d * Math.PI / 180.0;
    }


}
